package com.kjj.gift.service.impl;

import com.kjj.gift.dto.PhoneRegisterDto;
import com.kjj.gift.dto.RegisterPhoneValidateDto;
import com.kjj.gift.dto.SMSCodeDto;
import com.kjj.gift.exception.asserts.Assert;
import com.kjj.gift.exception.code.StatusCode;
import com.kjj.gift.service.ISmsService;
import com.kjj.gift.util.AjaxResult;
import com.kjj.gift.util.StrUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class SmsServiceImpl  implements ISmsService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${sms.code.register.resend-time}")
    private Integer resendTime;
    @Value("${sms.code.register.expire-time}")
    private Integer expireTime;
    @Override
    public AjaxResult sendSmsRegisterCode(SMSCodeDto smsCodeDto) {
        String phone = smsCodeDto.getPhone();
        //1 校验
        Assert.assertNotEmpty(phone, StatusCode.CODE_400);
        //2 判断原来的验证码是否存在
        String key ="register_sms_code_" + phone;
        String smsCodeOfRedis = (String) redisTemplate.opsForValue()
                .get(key);
        String code = "";
        if (StringUtils.isEmpty(smsCodeOfRedis)) {
            //2.1 如果不存在就产生新的
            code = StrUtils.getRandomString(6);
        }else{
            //2.2 如果存在，判断是否已过重发时间，如果没有过报错，过了直接原来的
            code = smsCodeOfRedis.split(":")[0];              //code:time
            String time = smsCodeOfRedis.split(":")[1];              //code:time
            if (System.currentTimeMillis()-new Date(Long.valueOf(time)).getTime() <= 60*1000*resendTime)  //60*1000
            {
                return AjaxResult.me().setSuccess(false).setMessage("非法请求，请勿乱搞！");
            }
        }
        //3 记录验证码到redis
        redisTemplate.opsForValue().set(key,code+":"+System.currentTimeMillis(),expireTime, TimeUnit.MINUTES);
        //4 调用短信接口进行发送
        String asd = (String) redisTemplate.opsForValue().get(key);
        String content = String.format("你的验证码为%s,请在%s分钟之内使用！", code, expireTime);
        //SmsUtil.sendSms(mobile,content);
        System.out.println("已经成功发送了一条短信,内容为"+content);
        return AjaxResult.me();
    }

    @Override
    public AjaxResult validateRegisterCode(RegisterPhoneValidateDto dto) {
        String phone = dto.getPhone();
        String validateCode = dto.getValidateCode();
        //从redis中获取验证码进行null判断
        String key ="register_sms_code_" + phone;
        String codeStr = (String) redisTemplate.opsForValue().get(key);
        Assert.assertNotEmpty(codeStr, StatusCode.CODE_400);
        //验证码比对
        String codeofRedis = codeStr.split(":")[0];
        Assert.assertEqualsIgnoreCase(validateCode,codeofRedis,StatusCode.CODE_400_CODE_NOT_EQUALS);
        return AjaxResult.me();
    }

}
